<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/5
 * Time: 上午10:19
 */
class BanUserService extends Service{

    public static function indexHeader(){
        return array(
                'id'                => array('text' => 'ID', 'rule' => 'int'),
    			'server_name'       => array('text' => '服务器'),
        		'ban_type'            => array('text' => '封禁类型'),
    			'status'            => array('text' => '操作'),
    			'data'              => array('text' => '封禁列表'),
        		'ban_date'          => array('text' => '截止日期'),
        		'reason'            => array('text' => '封禁原因'),
        		'create_user'       => array('text' => '添加人'),
    			'create_time'       => array('text' => '创建时间'),
    			'action'            => array('text' => '操作')
        );
    }
    
    public static function indexData($params){
    	$user = AccAuth::me();
    	if($user['IsAdmin'] != 1 && $user['AddBy'] != 1){
    		$params['create_user'] = $user['UserID'];
    	}
    	
    	$modBanUser = new ModBanUser();
    	$result = $modBanUser->select($params);
    
    	$banType = self::banType();
    	$userList = self::userList();
    	foreach($result as &$v){
    		$v['create_user'] = $userList[$v['create_user']];
    		$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
    		$v['ban_type'] = $banType[$v['ban_type']];
    		
    		if($v['status'] == 1){
    			$v['action'] = '<a href="javascript:;" name="untie" data-id="'.$v['id'].'">解封</a>';
    			$status = '封禁';
    		}else if($v['status'] == 0){  //解封的状态下才能进行删除
    			//$v['action'] = ' <a href="'.PubFunc::buildUrl('/oss/?r=banUser/delete').'&id='.$v['id'].'" confirm="是否要删除该条信息?">删除</a>';	
    			$status = '解封';
    		}
    		$v['status'] = $status;
    		
    		if($v['ban_date'] == 2000000000){
    			$v['ban_date'] = '永久';
    		}else{
    			$v['ban_date'] = date('Y-m-d', $v['ban_date']);
    		}
    		
    	}
    	unset($v);
    
    	return $result;
    }
    
    public static function doAdd($params){
    	if(PubFunc::checkEmpty($params, array('ban_type','data'))){
    		return array('state' => false, 'msg' => '缺少必要参数.');
    	}
    	$server_array = explode('-', $params['server']);
    	$params['server'] = $server_array[0];
    	//请求接口，接口成功再入库
    	$result = self::send($params);
    	//$result['state'] = true;
    	if($result['state']){
    		$user = AccAuth::me();
    		$modBanUser = new ModBanUser();
    		$params['ban_date'] = (isset($params['ban_forever']) || empty($params['ban_date'])) ? 2000000000 : strtotime($params['ban_date']);
    		$modBanUser->platform      = 'test';
    		$modBanUser->game          = $params['g'];
    		$modBanUser->server_id  = $server_array[0];
    		$modBanUser->server_name   = $server_array[1];
    		$modBanUser->ban_type    = $params['ban_type'];
    		$modBanUser->status       = $params['status'];
    		$modBanUser->data       = $params['data'];
    		$modBanUser->ban_date       = $params['ban_date'];
    		$modBanUser->reason       = $params['reason'];
    		$modBanUser->create_user   = $user['UserID'];
    		$modBanUser->create_time   = time();
    		$modBanUser->update_user   = $user['UserID'];
    		$modBanUser->update_time   = time();
    		$result = $modBanUser->save();
    		return array('state' => true, 'msg' =>'发送成功');
    	}else{
    		return array('state' => false, 'msg' => $result['msg']);
    	}
    
    }
    
    public static function delete($id){
    	if(empty($id)) return false;
    	$modBanUser = new ModBanUser();
    	$modBanUser->deleteByPk($id);
    	return true;
    }
    
    public static function get($id){
    	$modBanUser = new ModBanUser();
    	$obj = $modBanUser->findByPk($id);
    	$info = $obj->getAttributes();
    	return $info;
    }
    
    public static function doEdit($id, $params){
    	if(empty($id)) return false;
    
    	$user = AccAuth::me();
    
    	$modBanUser = new ModBanUser();
    	$update = array();
    	$update['status']       = $params['status'];
    	$update['update_user']  = $user['UserID'];
    	$update['update_time']  = time();
    
    	$modBanUser->updateByPk($id, $update);
    	return true;
    }
    
    
    public static function send($data){
    	$params = array(
    			'bantype'       => $data['ban_type'],
    			'list'          => $data['data'],
    			'actiontype'    => $data['status'],
    			'bandate'       => $data['ban_date'],
    			'ban_forever'   => $data['ban_forever'],
    			'reason'        => $data['reason'],
    	);
    	$params['bandate'] = (isset($params['ban_forever']) || empty($params['bandate'])) ? 2000000000 : strtotime($params['bandate']);
    	$params['list'] = CommonService::getStringFromMultiLine($params['list']);

    	unset($params['ban_forever']);
    
    	$api = new ApiService($data['server']);
    	return $api->ban($params);
    }

    public static function banType(){
        return array(
            '1'        => '按账号封禁',
//             '2'       => '按角色名封禁',
//             '3'       => '按设备号封禁',
//             '4'         => '按IP封禁',
        );
    }


}